
import 'package:flutter/material.dart';
import 'package:package_info_plus/package_info_plus.dart';
import 'package:permission_handler/permission_handler.dart';
import 'package:shared_preferences/shared_preferences.dart';

import 'app_constant.dart';

class Utils {
  Utils._();

  static Future<String> getAppVersion() async {
    PackageInfo packageInfo = await PackageInfo.fromPlatform();
    String versionName = packageInfo.version; // 版本号
    String buildNumber = packageInfo.buildNumber; // 构建号

    print('Version Name: $versionName');
    print('Build Number: $buildNumber');
    return versionName;
  }

  static Future<String> getSharedPreferenceString(String key) async {
    SharedPreferences sp = await SharedPreferences.getInstance();
    var string = sp.getString(key);

    print('key: $string');
    return string;
  }

  static Future<List<String>> getSharedPreferenceStringList(String key) async {
    SharedPreferences sp = await SharedPreferences.getInstance();
    var strs = sp.getStringList(key);

    print('key: $strs');
    return strs;
  }

  static void logOut() async {
    var sp = await SharedPreferences.getInstance();
    sp.remove(Constants.token);
  }

  static String getStringisEmpty(String key) {
    return key == null ? "" : key;
  }

  static String getStringisInteger(int key) {
    return key == null ? "" : "${key}";
  }
  static String getStringisDouble(double key) {
    return key == null ? "" : "${key}";
  }

  static bool isEmpty(String key) {
    if(key==null){
      return true;
    }
    if(key==""){
      return true;
    }
    return false;
  }

  static bool isBool(bool key) {
    if(key==null){
      return false;
    }
    return false;
  }

  static Future<String> scanBarcode(BuildContext context) async {
    // String barcodeScanRes;
    // // Platform messages may fail, so we use a try/catch PlatformException.
    // try {
    //   barcodeScanRes = await FlutterBarcodeScanner.scanBarcode(
    //       '#ff6666', 'Cancel', true, ScanMode.QR);
    //   print(barcodeScanRes);
    // } on PlatformException {
    //   barcodeScanRes = 'Failed to get platform version.';
    // }
    return "";
  }

  static Future<bool> requestCameraPermission() async {
    final status = await Permission.camera.request();
    return status == PermissionStatus.granted;
  }

  static int getIntegerisEmpty(int key) {
    return key == null ? 0 : key;
  }

  static bool isInteger(String str) {
    return int.tryParse(str) != null;
  }

  static bool isDouble(String str) {
    return double.tryParse(str) != null;
  }

}
